import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-07-17
 * Time: 18:36
 */

public class test_1 {
    public int lengthOfLIS(int[] nums) {
        // 1. 创建 dp 表
        // 2. 初始化
        // 3. 填表
        // 4. 返回值
        int n = nums.length;
        int[] dp = new int[n];
        for (int i = 0; i < n; i++) dp[i] = 1;
        int ret = 1;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++)
                if (nums[j] < nums[i])
                    dp[i] = Math.max(dp[j] + 1, dp[i]);
            ret = Math.max(ret, dp[i]);
        }
        return ret;
    }

    public int findNumberOfLIS(int[] nums) {
        // 1. 创建 dp 表
        // 2. 初始化
        // 3. 填表
        // 8 // 4. 返回值

        int n = nums.length;
        int[] len = new int[n];

        int[] count = new int[n];
        for (int i = 0; i < n; i++) len[i] = count[i] = 1;
        int retlen = 1, retcount = 1;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[j] < nums[i]) {
                    if (len[j] + 1 == len[i]) // 计数
                        count[i] += count[j];
                    else if (len[j] + 1 > len[i]) // 重新计数
                    {
                        len[i] = len[j] + 1;
                        count[i] = count[j];
                    }
                }
            }
            if (retlen == len[i]) retcount += count[i];
            else if (retlen < len[i]) // 重新计数
            {
                retlen = len[i];
                retcount = count[i];
            }
        }
        return retcount;
    }

    public int findLongestChain(int[][] pairs) {
        // 预处理
        Arrays.sort(pairs, (a, b) -> a[0] - b[0]);
        int n = pairs.length;
        int[] dp = new int[n]; // 建表
        for(int i = 0; i < n; i++) dp[i] = 1; // 初始化
        int ret = 1;
        for(int i = 1; i < n; i++)
        {
            for(int j = 0; j < i; j++)
                if(pairs[j][1] < pairs[i][0])
                    dp[i] = Math.max(dp[j] + 1, dp[i]);
            ret = Math.max(ret, dp[i]);
        }
        return ret;
    }
    public int longestSubsequence(int[] arr, int difference)
    {
        // 创建⼀个哈希表，在哈希表中做 dp
        Map<Integer, Integer> hash = new HashMap<Integer, Integer>(); // arr[i],
        int ret = 1;
        for(int a : arr)
        {
            hash.put(a, hash.getOrDefault(a - difference, 0) + 1);
            ret = Math.max(ret, hash.get(a));
        }
        return ret;
    }
    public int lenLongestFibSubseq(int[] arr) {
        // 优化：将 数组中的元素 + 下标 绑定在⼀起，扔到哈希表中
        Map<Integer, Integer> hash = new HashMap<Integer, Integer>();
        int n = arr.length;
        for (int i = 0; i < n; i++) hash.put(arr[i], i);
        int[][] dp = new int[n][n];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                dp[i][j] = 2;

        int ret = 2;
        for (int j = 2; j < n; j++) // 固定最后⼀个数
            for (int i = 1; i < j; i++) // 枚举倒数第⼆个数
            {
                int a = arr[j] - arr[i];
                if (a < arr[i] && hash.containsKey(a))
                    dp[i][j] = dp[hash.get(a)][i] + 1;
                ret = Math.max(ret, dp[i][j]);
            }
        return ret < 3 ? 0 : ret;
    }
}
